package com.fire.threadpool.interceptLongTimeThread;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @date 2022/3/24
 * @author zwd
 * @email zwd@hhh.com
 */
public class DelayTask implements Delayed{
	private String name ;
	private long time;
	private long start = System.currentTimeMillis();

	DelayTask(String name, long time) {
		this.name = name;
		this.time = time;
	}

	@Override
	public long getDelay(TimeUnit unit) {
		return unit.convert((start+time) - System.currentTimeMillis(),TimeUnit.MILLISECONDS);

	}

	@Override
	public int compareTo(Delayed o) {
		return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));

	}
	@Override
	public String toString() {
		return "DelayTask{" +
				"name='" + name + '\'' +
				", time=" + time +
				'}';
	}
}
